<?php

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\knowledge\CategoryModel;
use bc\model\knowledge\TagModel;
use bc\util\backend\Page;

class KnowledgeTagController extends BackendController
{
	/**
	 * 
	 */
	public function actionIndex()
	{
		$model = new TagModel();
		
		$params = '1 = 1';
		
		$tag_name = $this->get('tag_name');
		
		if($tag_name)
		{
			$params .= ' AND tag_name LIKE "%' . $tag_name . '%"'; 
		}
                $params .=' AND tag_status = 1';
		
		$page = $this->get('page', 1);
                
                $model->autoId = 'tag.tag_id';
                    
                $list = $model->getBackendList($params, $page, self::PAGE_SIZE);

                $total = $model->getBackendTotal($params);
                //var_dump($total);die();
//                foreach($list as  $k=>$v){
//                    $b[$v['tid']][] = $v['cname'];
//                };
//                
//                $v=""; 
//                foreach($b as $key=>$value){
//                            if( count($value) == 2 ){
//                                for($i=0;$i<2;$i++){
//                                    $v .= $value[$i].',';                                  
//                                }
//                                $v = substr($v,0,-1);
//                                $b[$key]['cname'] = $v;
//                                $v="";
//                            }else{
//                                $b[$key]['cname'] =$value[0];
//                            }
//                          
//                     }
//                 
//               foreach($list as  $k1=>$v1){                     
//                    $b[$v1['tid']]['tag_type'] = $v1['tag_type'];
//                    $b[$v1['tid']]['tname'] = $v1['tname'];
//                    $b[$v1['tid']]['tid'] = $v1['tid'];
//                };
//		
                $pagination = new Page($total, self::PAGE_SIZE);
		
                //$view['b'] = $b;
		$view['list'] = $list;
		$view['pagination'] = $pagination;
		$view['tag_name'] = $tag_name;
		
		return $this->render('index', $view);
	}
		
	/**
	 * 
	 */
	public function actionAdd()
	{
		$categoryModel = new CategoryModel();
		
		$categorys = $categoryModel->getList(['parent_id' => 0, 'cate_status' => 1]);
		
		$view['categorys'] = $categorys;
		return $this->render('add', $view);
	}
	
	
	/**
	 * 
	 */
	public function actionEdit()
	{
		$id = $this->get('id');
		$tagModel = new TagModel();
		
		$tag = $tagModel->getRowById($id);
		if(!$tag)
			$this->redirect('/');
		$categoryModel = new CategoryModel();		
		$categorys = $categoryModel->getList(['parent_id' => 0, 'cate_status' => 1]);
		
                $cateTagModel = new \bc\model\knowledge\KnowledgecategorytagModel();
                $cateTags = $cateTagModel ->getList(['tag_id'=> $id,'bc_knowledge_category_tag.category_tag_status' => 1]);
                
		$view['categorys'] = $categorys;
		$view['row'] = $tag;
                $view['tags'] = $cateTags;
		return $this->render('edit', $view);
	}
	
	
	/**
	 * 
	 */
	public function actionSave()
	{
		$tagModel = new TagModel();
		$id = $this->post('id');
		$data['tag_name'] = $this->post('tag_name');
                $data['tag_status'] = 1;
                $data['tag_type'] = $this->post('type');
                $info['category_id'] = $_POST['category_id'];
		if($id)
		{       
                    	$data['update_time'] = date('Y-m-d H:i:s');
			$tagModel->modify($data, $id);
                        
                        $cateTagModel = new \bc\model\knowledge\KnowledgecategorytagModel();
                                $tagsList = $cateTagModel->getList(['tag_id'=>$id]);
                                
                                //var_dump($tagsList);die();
                                $info['category_tag_status'] = 0;
                                $info['update_time'] = date('Y-m-d H:i:s');
                                $cateTagModel->tractionModify($id, $tagsList, $info['category_tag_status'],$info['update_time']);
                                
                                 if(is_array( $info['category_id'] ))
                                 {  
                                  
                                     $info['category_tag_status'] = 1;
                                     $info['create_time'] = date('Y-m-d H:i:s');
                                     $cateTagModel ->tractionInsert($id, $info['category_id'], $info['category_tag_status'],$info['create_time']);                                       
                                    
                                 }else{

                                     $info['tag_id'] = $id;
                                     $info['category_id'] = $info['category_id'];
                                     $info['category_tag_status'] = 1;
                                     $info['create_time'] = date('Y-m-d H:i:s');
                                     $cateTagModel->add($info);
                                 }
		}
		else 
		{       $data['create_time'] = date('Y-m-d H:i:s');
			$primaryId = $tagModel->add($data);
                        
                        $cateTagModel = new \bc\model\knowledge\KnowledgecategorytagModel();
                        if(is_array( $info['category_id'] ))
                                 {                              
                                     $info['category_tag_status'] = 1;
                                     $info['create_time'] = date('Y-m-d H:i:s');
                                     $cateTagModel ->tractionInsert($primaryId, $info['category_id'], $info['category_tag_status'],$info['create_time']);                                       
                                    
                                 }else{

                                     $info['tag_id'] = $primaryId;
                                     $info['category_id'] = $info['category_id'];
                                     $info['category_tag_status'] = 1;
                                     $info['create_time'] = date('Y-m-d H:i:s');
                                     $cateTagModel->add($info);
                                 }
		}
		
		$response['msg '] = '修改成功';
		$response['url'] = '/knowledge-tag/index';
		$this->forward($response);
		
	}
        
        public function  actionDelete(){
                       $id = $this->get('id');                      

                       $model = new TagModel();
                       $data['tag_status'] = 0;
                       $model ->modify($data, $id);
                       
                       $output['msg'] = '操作成功!';
                       $output['url'] = '/knowledge-tag/index';
                       $this->forward($output);
        }

                /**
	 * Ajax获取2级分类
	 */
	public function actionAjaxcate()
	{
		$parent_id = (int)$this->post('parent_id');
		
		$categorys = [];
		
		if($parent_id > 0)
		{
			$categoryModel = new CategoryModel();
			$categorys = $categoryModel->getList(['parent_id' => $parent_id, 'cate_status' => 1]);
		}
		
		
		echo json_encode($categorys);
		exit;
	}
}

